<?php
require_once dirname(__FILE__) .'/cse-init.php';

$view = Cinnamon::registry('view');
$params = Cinnamon::registry('params');
$session = Cinnamon::registry('session');

switch ( $params->action ) {
	case 'login':
		if ( $session->login($params->cseauth_login, $params->cseauth_pass) ) {
			if ( !empty($params->url) ) {
				header('Location: '. urldecode($params->url));
			} else {
				header('Location: index.php');
			}
		} else {		// login failed
			header('Location: cse-login.php?failed=true&url='. $params->url);
		}
		break;
	default:
		if ( $session->isAuthenticated() ) {
			header('Location: index.php');
		} else {
			if ( strcmp($params->failed, 'true') == 0 ) {
				$view->addMessage(View::ERROR, 'Please specify the correct username and password');
			}
			$view->setVariable('url', $params->url);
			$view->render('login.php');	
		}
		break;
}
?>